//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;
 
namespace STT.Business
{
    public class BTbTinTuc
    {
//-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbTinTuc_SelectAll", null);
            return all;
        }
        public static DataTable SelectTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbTinTuc_SelectTop", pr);
            return all;
        }
//-----------------------------------------------------------//
        public static void Insert(ETbTinTuc OTbTinTuc)
        {
            SqlParameter[] pr = new SqlParameter[7];
            pr[0] = new SqlParameter(@"TieuDe", OTbTinTuc.TieuDe);
            pr[1] = new SqlParameter(@"NDTomTat", OTbTinTuc.NDTomTat);
            pr[2] = new SqlParameter(@"NDChiTiet", OTbTinTuc.NDChiTiet);
            pr[3] = new SqlParameter(@"MaLoaiTT", OTbTinTuc.MaLoaiTT);
            pr[4] = new SqlParameter(@"NguoiDang", OTbTinTuc.NguoiDang);
            pr[5] = new SqlParameter(@"NgayDang", OTbTinTuc.NgayDang);
            pr[6] = new SqlParameter(@"LinkFile", OTbTinTuc.LinkFile);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbTinTuc_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(ETbTinTuc OTbTinTuc)
        {
            SqlParameter[] pr = new SqlParameter[8];
            pr[0] = new SqlParameter(@"MaTT", OTbTinTuc.MaTT);
            pr[1] = new SqlParameter(@"TieuDe", OTbTinTuc.TieuDe);
            pr[2] = new SqlParameter(@"NDTomTat", OTbTinTuc.NDTomTat);
            pr[3] = new SqlParameter(@"NDChiTiet", OTbTinTuc.NDChiTiet);
            pr[4] = new SqlParameter(@"MaLoaiTT", OTbTinTuc.MaLoaiTT);
            pr[5] = new SqlParameter(@"NguoiDang", OTbTinTuc.NguoiDang);
            pr[6] = new SqlParameter(@"NgayDang", OTbTinTuc.NgayDang);
            pr[7] = new SqlParameter(@"LinkFile", OTbTinTuc.LinkFile);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbTinTuc_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int MaTT)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaTT",MaTT);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbTinTuc_Delete", pr);
        }
//-----------------------------------------------------------//
        public static ETbTinTuc SelectByID(int MaTT)
        {
            ETbTinTuc OTbTinTuc = new ETbTinTuc();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaTT",MaTT);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"TbTinTuc_SelectByID", pr);
            if (idr.Read())
              OTbTinTuc = GetOneTbTinTuc(idr);
            idr.Close();
            idr.Dispose();
            return OTbTinTuc;
        }
//-----------------------------------------------------------//
        private static ETbTinTuc GetOneTbTinTuc(IDataReader idr)
       {
            ETbTinTuc OTbTinTuc = new ETbTinTuc();
                if (idr["MaTT"] != DBNull.Value)
                    OTbTinTuc.MaTT = (int)idr["MaTT"];
                if (idr["TieuDe"] != DBNull.Value)
                    OTbTinTuc.TieuDe = (string)idr["TieuDe"];
                if (idr["NDTomTat"] != DBNull.Value)
                    OTbTinTuc.NDTomTat = (string)idr["NDTomTat"];
                if (idr["NDChiTiet"] != DBNull.Value)
                    OTbTinTuc.NDChiTiet = (string)idr["NDChiTiet"];
                if (idr["MaLoaiTT"] != DBNull.Value)
                    OTbTinTuc.MaLoaiTT = (int)idr["MaLoaiTT"];
                if (idr["NguoiDang"] != DBNull.Value)
                    OTbTinTuc.NguoiDang = (string)idr["NguoiDang"];
                if (idr["NgayDang"] != DBNull.Value)
                    OTbTinTuc.NgayDang = (DateTime)idr["NgayDang"];
                if (idr["LinkFile"] != DBNull.Value)
                    OTbTinTuc.LinkFile = (string)idr["LinkFile"];
            return OTbTinTuc;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int MaTT)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaTT",MaTT);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbTinTuc_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<ETbTinTuc> ListAll()
        {
            List<ETbTinTuc> list = new List<ETbTinTuc>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbTinTuc_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneTbTinTuc(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }

        public static List<ETbTinTuc> ListTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            List<ETbTinTuc> list = new List<ETbTinTuc>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbTinTuc_SelectAll", pr);
            while (idr.Read())
              list.Add(GetOneTbTinTuc(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
    }
 
}